﻿using System;
using System.Collections.Generic;
using UnityEngine;

public class TaskMgr
{
    private static TaskMgr instance = null;
    public static TaskMgr Instance
    {
        get
        {
            if (instance == null)
                instance = new TaskMgr();
            return instance;
        }
    }

    public TaskMgr()
    {
        id2task = new Dictionary<int, TaskBase>();
        foreach (var ConfigTaskElement in ConfigTask.id2Element.Values)
        {
            TaskBase taskBase = null;
            switch (ConfigTaskElement.Type)
            {
                case TaskType.Item:
                    var taskItem = new TaskItem();
                    taskItem.itemTypeId = ConfigTaskElement.Key;
                    taskItem.itemCount = ConfigTaskElement.Value;
                    taskItem.gridX = ConfigTaskElement.X;
                    taskItem.gridY = ConfigTaskElement.Y;
                    taskBase = taskItem;
                    break;
                default:
                    Debug.LogError("unhandle TaskType:" + ConfigTaskElement.Type);
                    break;
            }
            id2task[ConfigTaskElement.Id] = taskBase;
        }
    }

    private Dictionary<int, TaskBase> id2task;

    public TaskBase GetTaskItemById(int id)
    {
        TaskBase taskBase;
        if(!id2task.TryGetValue(id, out taskBase))
        {
            Debug.LogError("Can not find id:" + id);
        }
        return taskBase;
    }
}
